<?php
namespace app\cdyoa\controller;
use think\Db;
class Customer extends Base
{
    
    public function lists(){
        if( is_post() ){
            $page     = input('page');
            $status   = input('status');
            $keyword  = input('keyword');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            $sql = "
                SELECT
                    [1]
                FROM
                    customer
                WHERE 
                    1 = ?
                ";
            $param = [1];
            if ( $status != '100all' ) {
                $sql .= " and status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and (username like ? or name like ?)";
                $param[] = "%{$keyword}%";
                $param[] = "%{$keyword}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and create_time <= ?";
                $param[] = strtotime($max_time);
            }
            //分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
            $page_data   = com_page_data($total, $page,  $page_size);
            
            //列表
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by id desc limit $start, $page_size";
            $field       = "*";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                $list[$k]['status']         =  com_page( ['url'=>url("c/edit"),'val'=>$v['status'],'post'=>['t'=>'customer','id'=>$v['id'],'f'=>'status']],'iphone' );
                $list[$k]['sort']           =  com_page( ['url'=>url("c/edit"),'val'=>$v['sort'],'post'=>['t'=>'customer','id'=>$v['id'],'f'=>'sort']],'input' );
                $list[$k]['action']         =  com_page(['title'=>'编辑客户','url'=>url("customer/edit","id=".$v['id']),'ico'=>'edit','width'=>600,'height'=>500]);
                $list[$k]['action']         .=  com_page(['title'=>'删除客户','url'=>url("customer/delete"),'post'=>['t'=>'customer','id'=>$v['id']],'ico'=>'del3', 'confirm'=>"你确定要删除[{$v['name']}]吗?"]);
            }
            $com_body    = com_build_table($list, array("checkbox","id", "name" ,"username","country", "phone", "address", "sort", "status", "create_time","action"));            
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data,'suc'=>['js_action'=>'function.iphone']]);
        }
        $data = [
            'status'    => ['100all'=>'全部状态'] + c('type.status'),
            'nav'       => ['客户管理', '列表'],
        ];
        return view('', $data);
    }

    public function add(){
        if( is_post() ){
            $data = com_data(input());
            $rule = [
                'name'      =>  'require',
            ];
            $msg = [
                'name.require'       =>  '姓名不能为空',
            ];
            com_validate( $rule, $msg, $data, true );
            $data['create_time'] = time();
            Db::name('customer')->insert($data);
            $config = [
                'msg' => '添加客户成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        } 
        return view();
    }

    //编辑
    public function edit( $id='' ){
        if( is_post() ){
            $data = com_data(input());
            $rule = [
                'name'      =>  'require',
            ];
            $msg = [
                'name.require'       =>  '姓名不能为空',
            ];
            com_validate( $rule, $msg, $data, true );
            $data['update_time'] = time();
            Db::name('customer')->update($data);
            $config = [
                'msg' => '编辑客户成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        $data = [
            'info'=> Db::name('customer')->find($id),
            'id'  => $id,
        ];
        return view('', $data);
    }

    //删除
    public function delete(){
        if( is_post() ){
            $id  =  input('id');
            $rs = Db::name('in_shou')->where('customer_id',$id)->find();
            if($rs){
                com_error(['已有数据不能删除']);
            }else{
                Db::name('customer')->delete($id);
            }   
            $config = [
                'suc' => [
                    'msg'       => '删除成功!',
                    'js_action' => 'alert|reload',
                ],
            ];
            com_check($config);
        }
    }
    
    //批量删除
    public function delall(){
        if( is_post() ){
            $ids = input('ids/a');
            foreach($ids as $id){
                $rs = Db::name('in_shou')->where('customer_id',$id)->find();
                if($rs){
                    com_error(['已有数据不能删除']);
                }else{
                    Db::name('customer')->delete($id);
                }
            }
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check($config);
        }
    }
}
